package com.iie.bigVideo.controller;


import com.iie.bigVideo.entity.Annex;
import com.iie.bigVideo.entity.dto.ImageDto;
import com.iie.bigVideo.entity.dto.VideoDto;
import com.iie.bigVideo.service.AnnexService;
import com.iie.bigVideo.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XXX
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/annex")
@CrossOrigin //跨域
public class AnnexController {
    @Autowired
    private AnnexService annexService;

    /**
     * 查询全附件
     *
     * @param current 页码
     * @param size    个数
     * @return
     */
    @GetMapping("/queryAnnexList/{current}/{size}")
    public ResultVO queryVideoList(@PathVariable("current") int current, @PathVariable("size") int size) {
        HashMap<String, String> queryAnnexParamHashMap = new HashMap<>();
        queryAnnexParamHashMap.put("current", String.valueOf(current));
        queryAnnexParamHashMap.put("size", String.valueOf(size));
//        queryAnnexParamHashMap.put("sort", sort);
        return annexService.queryAnnexList(queryAnnexParamHashMap);
    }

    /**
     * @param annexName        附件名
     * @param catalogId        课件ID
     * @param videoImgMainFile 视频封面图
     * @param videoFile        视频
     * @param imgMainFile      图集封面图
     * @param imgFile          图集 (多个图片)
     * @return
     */
    @PostMapping("/addAnnexObject")
    public ResultVO addAnnexObject(@RequestParam(value = "annexName") String annexName,
                                   @RequestParam(value = "imageName") String imageName,
                                   @RequestParam(value = "catalogId") String catalogId,
                                   @RequestParam(value = "videoImgMainFile", required = false) MultipartFile videoImgMainFile,
                                   @RequestParam(value = "videoFile", required = false) MultipartFile videoFile,
                                   @RequestParam(value = "imgMainFile", required = false) MultipartFile imgMainFile,
                                   @RequestParam(value = "imgFile", required = false) MultipartFile[] imgFile) {
        Map<String, Object> fileWithObjHashMap = new HashMap<>();
        Annex annex = new Annex();
        annex.setAnnexName(annexName);
        annex.setCatalogId(catalogId);
        fileWithObjHashMap.put("annex", annex);//附件对象
        fileWithObjHashMap.put("videoImgMainFile", videoImgMainFile);
        fileWithObjHashMap.put("videoFile", videoFile);
        fileWithObjHashMap.put("imgMainFile", imgMainFile);
        fileWithObjHashMap.put("imageName", imageName);//图集名称
        //图集
        if (imgFile != null) {
            try {
                //转成file类型
                List<ImageDto> imageDtoArrayList = new ArrayList<ImageDto>();
                for (int i = 0; i < imgFile.length; i++) {
                    ImageDto imageDto = new ImageDto();
                    imageDto.setImageFile(imgFile[i]);//文件
                    imageDto.setImageOrder(String.valueOf(i + 1));
                    imageDtoArrayList.add(imageDto);//添加到img对象
                }
                fileWithObjHashMap.put("imgFile", imageDtoArrayList);//集合
            } catch (Exception e) {
                //进行修改
                return null;
            }
        }
        return annexService.addAnnexObject(fileWithObjHashMap);
    }


    /**
     * 删除附件
     *
     * @param annexId 附件ID
     * @return
     */
    @GetMapping("/deleteAnnex/{annexId}")
    public ResultVO deleteAnnex(@PathVariable("annexId") int annexId) {
        return annexService.deleteAnnex(annexId);
    }

    /**
     * 通过 附件 ID 查询
     *
     * @param annexId
     * @return
     */
    @GetMapping("/queryByAnnexId/{annexId}")
    public ResultVO<VideoDto> queryByAnnexId(@PathVariable("annexId") int annexId) {
        return annexService.queryByAnnexId(annexId);
    }

    /**
     * 修改附件
     *
     * @param annexId
     * @param annexName
     * @param imageName
     * @param catalogId
     * @param videoImgMainFile
     * @param videoFile
     * @param imgMainFile
     * @param imgFile
     * @return
     */
    @PostMapping("/updateAnnexObject")
    public ResultVO updateAnnex(@RequestParam(value = "annexId") String annexId,
                                @RequestParam(value = "annexName") String annexName,
                                @RequestParam(value = "imageId") String imageId,
                                @RequestParam(value = "imageName") String imageName,
                                @RequestParam(value = "catalogId", required = false) String catalogId,
                                @RequestParam(value = "videoImgMainFile", required = false) Object videoImgMainFile,
                                @RequestParam(value = "videoFile", required = false) Object videoFile,
                                @RequestParam(value = "imgMainFile", required = false) Object imgMainFile,
                                @RequestParam(value = "imgFile", required = false) MultipartFile[] imgFile) {
        Annex annex = new Annex();
        annex.setId(Integer.parseInt(annexId));//附件id
        annex.setAnnexName(annexName);//附件名
        annex.setCatalogId(catalogId);//课件id
        annex.setAnnexUpdatedTime(new Date());//附件修改时间
        Map<String, Object> annexMap = new HashMap<>();
        annexMap.put("annex", annex);//附件信息
        annexMap.put("imageId", imageId);//图集带封面的id
        annexMap.put("imageName", imageName);//图集名称
        if (videoImgMainFile != null || videoFile != null || imgMainFile != null || imgFile != null) {
            // 处理至少一个文件不为空的情况
            if (videoImgMainFile instanceof MultipartFile && !((MultipartFile) videoImgMainFile).isEmpty()) {
                // 处理 videoImgMainFile 不为空的情况
                MultipartFile videoImgMain = (MultipartFile) videoImgMainFile;
                // 进行文件处理逻辑，比如保存到磁盘或数据库
                annexMap.put("videoImgMainFile", videoImgMain);
            }

            if (videoFile instanceof MultipartFile && !((MultipartFile) videoFile).isEmpty()) {
                // 处理 videoFile 不为空的情况
                MultipartFile video = (MultipartFile) videoFile;
                // 进行文件处理逻辑
                annexMap.put("videoFile", video);
            }

            if (imgMainFile instanceof MultipartFile && !((MultipartFile) imgMainFile).isEmpty()) {
                // 处理 imgMainFile 不为空的情况
                MultipartFile imgMain = (MultipartFile) imgMainFile;
                // 进行文件处理逻辑
                annexMap.put("imgMainFile", imgMain);
            }
            //图集
            if (imgFile != null) {
                try {
                    //转成file类型
                    List<ImageDto> imageDtoArrayList = new ArrayList<ImageDto>();
                    for (int i = 0; i < imgFile.length; i++) {
                        ImageDto imageDto = new ImageDto();
                        imageDto.setImageFile(imgFile[i]);//文件
                        imageDto.setImageOrder(String.valueOf(i + 1));
                        imageDtoArrayList.add(imageDto);//添加到img对象
                    }
                    annexMap.put("imgFile", imageDtoArrayList);//集合
                } catch (Exception e) {
                    //进行修改
                    return annexService.updateAnnex(annexMap);
                }
            }

        }
        return annexService.updateAnnex(annexMap);
    }
}

